var service = require('../../../utils/utilService.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isLoading:{
      type: Boolean,
      value: true
    },
    dataSource: {
      type: Array,
      value: null,
      observer: function (newValue, oldValue) {
        for (var index in newValue) {
          var id = ""
          if (newValue[index].id != null) {
            id = newValue[index].id
          } else {
            id = newValue[index].storyId
          }

          if (this.data.favIds.indexOf(id) != -1) {

            if (newValue[index].isStore == 0) {
              newValue[index].storeCount += 1
              newValue[index].isStore = 1
            }

          }
          if (this.data.favDelIds.indexOf(id) != -1) {

            if (newValue[index].isStore == 1) {
              newValue[index].isStore = 0
              var count = newValue[index].storeCount - 1
              newValue[index].favorityCount = count < 0 ? 0 : count
            }

          }

          if (this.data.likeIds.indexOf(id) != -1) {

            if (newValue[index].isAdmire == 0) {
              newValue[index].admireCount += 1
              newValue[index].isAdmire = 1
            }

          }
        }
        this.setData({ items: newValue })
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    items: [],
    isScroll: true,
    likeIds: [],
    favIds: [],
    favDelIds: []
  },

  ready: function () {
    // this.videoView = this.selectComponent('#videoView', this)
  },

  /**
   * 组件的方法列表
   */
  methods: {
    favCallBack(data) {
      var store = data.detail.isStore
      var id = data.detail.id
      var index = this.data.favIds.indexOf(id)
      var delIndex = this.data.favDelIds.indexOf(id)

      if (store == 0) {  //删除 
        if (index != -1) {
          this.data.favIds.splice(index, 1)
        }
        if (delIndex == -1) {
          this.data.favDelIds.splice(-1, 0, id)
        }
      } else { //添加
        if (index == -1) {
          this.data.favIds.splice(-1, 0, id)
        }
        if (delIndex != -1) {
          this.data.favDelIds.splice(index, 1)
        }
      }
      this.triggerEvent("favCallBack", data.detail)
    },
    likeCallBack(data) {
      var store = data.detail.isAdmire
      var id = data.detail.id
      var index = this.data.likeIds.indexOf(id)

      if (store == 0) {  //删除 
        if (index != -1) {
          this.data.likeIds.splice(index, 1)
        }
      } else { //添加
        if (index == -1) {
          this.data.likeIds.splice(-1, 0, id)
        }
      }
    },
    videoViewDimiss() {
      this.setData({ isScroll: true })
    },
    showInfo(data) {
      this.triggerEvent("showInfo", data)
    },
    showVideo(data) {
      this.setData({ isScroll: false })
      var videoView = this.selectComponent("#videoView", this)
      videoView.setData({ url: data.detail })
      videoView.show()
    },
    upper: function (e) {
      this.triggerEvent("upper", this.properties.activeIndex)
    },
    lower: function (e) {
      this.triggerEvent("lower", this.data.activeIndex)
    },
  }
})
